/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.display.ui.scroller {
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.display.service.MLMC;
	import com.googlecode.milib.vo.MLEvent;

	import flash.display.MovieClip;

	/**
	 * @author Marek Brun
	 */
	public class AbstractMCScroler extends MLBroadcastClass implements IScroller {
		
		protected var mlmc:MLMC;
		protected var scrolled:IScrollable;
		
		public function get event_VisibletoTotalChanged():String { return 'VisibletoTotalChanged'; }
		
		public function AbstractMCScroler(mc:MovieClip) {
			mlmc=MLMC.forInstance(mc);
		}
		
		/**
		 * Sets the slider in position
		 * 
		 * @param position number from 0 to 1
		 * @param visibleToTotal dependence of visible content (eg. text) to total length (visible/total)
		 */
		/*abstract*/ public function setPosition(position:Number, visibleToTotal:Number):void {}
		
		/**
		 * @return position number from 0 to 1 (determined by slider positin)
		 */
		/*abstract*/ public function getPosition():Number { return NaN; }
		
		/*abstract*/ public function setLength(newLength:Number):void {}		/*abstract*/ public function getLength():Number { return NaN; }
		/*abstract*/ public function getVisibleToTotal():Number { return NaN; }
		
		public function get mc():MovieClip { return mlmc.mc; };
		
		protected function update():void {
			setPosition(scrolled.getScroll(), scrolled.getVisibleToTotal());
		}
		
		public function setScrolled(scrolled:IScrollable):void {
			if(this.scrolled==scrolled){ return; }			unsetScrolled();
			this.scrolled=scrolled;
			this.scrolled.addEventListener(this.scrolled.event_ScrollChanged, onScrollChanged);
			update();
		}
		
		public function unsetScrolled():void {
			if(scrolled){
				scrolled.removeEventListener(scrolled.event_ScrollChanged, onScrollChanged);
				scrolled=null;
			}
		}
		
//********************************************************************************************
//		events for AbstractMCScroler
//********************************************************************************************
		protected function onScrollChanged(event:MLEvent):void {
			update();
		}
		
	}
}
